<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="cn.java79.quiz.util.MybatisUtil" %>
<%@ page import="cn.java79.quiz.dao.UserMapper" %>
<%@ page import="cn.java79.quiz.entity.User" %>
<%@ page import="java.util.List" %>
<%@ page import="cn.java79.quiz.service.UserService" %>
<%@ page import="cn.java79.quiz.entity.Admin" %>
<%@ page import="cn.java79.quiz.util.StrUtil" %>
<%@ page import="cn.java79.quiz.pojo.AjaxPage" %><%--
  Created by IntelliJ IDEA.
  User: oldliu
  Date: 2022/8/22
  Time: 17:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>所有虎客刷题系统-学生</title>
    <script src="js/jquery-3.4.1.min.js"></script>
    <style>
        table th{
            background-color: lightcyan;
        }
        table td{
            background-color: lightgrey;
        }
    </style>
</head>
<body>
<%@include file="/checkLogin.jsp"%>
<%--<p>
    欢迎管理员 <%=request.getParameter("login")%>
</p>--%>
  <h1>所有用户</h1>
  <a href="adduser.jsp">新用户注册</a>
<%
    String realname=StrUtil.trim(request.getParameter("realname"));
    int pageNo= StrUtil.toInt(request.getParameter("pageNo"),1);
    AjaxPage<User> p= UserService.ME.search(realname,null,null,pageNo);
%>
<p align="center">
    <form action="users.jsp" id="searchForm">
    <input type="hidden" name="pageNo" value="<%=p.getPage()%>"/>
      <input type="search" name="realname" value="<%=realname%>" placeholder="请输入用户名"/>
      <input type="search" name="loginname" placeholder="请输入登录名"/>
      <input type="search" name="phone" placeholder="请输入手机名"/>
    <input type="submit" value="搜索" id="search"/>
    </form>
</p>
  <table border="1" cellpadding="0" cellspacing="0" align="center">
      <tr>
          <th>编号</th>
          <th>登录名</th>
          <th>姓名</th>
          <th>手机号</th>
          <th>操作</th>
      </tr>
<%
//    SqlSession s= MybatisUtil.get();
//    UserMapper mapper=s.getMapper(UserMapper.class);
//    List<User> list = mapper.search();
//    MybatisUtil.close(s);

    for (User u:p.getData()){
    //div (ul  li)*

%>
      <tr>
          <td><%=u.getId()%></td>
          <td><%=u.getLoginname()%></td>
          <td><%=u.getRealname()%></td>
          <td><%=u.getPhone()%></td>
          <td><a onclick="return confirm('真的要删除<%=u.getRealname()%>?')" href="delUser.jsp?id=<%=u.getId()%>">删除</a>
              <a href="updateUser.jsp?id=<%=u.getId()%>">修改</a>
              <a href="resetUserPass.jsp?id=<%=u.getId()%>">重置密码</a>
          </td>

      </tr>
<%}%>
  </table>
<p align="center" id="pager">
    <a href="users.jsp?pageNo=1" data-id="1">首页</a>
    <%
        if(p.getPage()>1){
    %>
    <a href="users.jsp?pageNo=<%=p.getPage()-1%>" data-id="<%=p.getPage()-1%>">上一页</a>
    <%}%>
    <input type="number" min="1" value="<%=p.getPage()%>" name="pageNo" style="width: 50px"/>/<%=p.getPageNums()%><button type="button" id="goto">直达</button>
   <% if(p.getPage()<p.getPageNums()){%>
    <a href="users.jsp?pageNo=<%=p.getPage()+1%>" data-id="<%=p.getPage()+1%>">下一页</a>
    <%}%>
    <a href="users.jsp?pageNo=<%=p.getPageNums()%>" data-id="<%=p.getPageNums()%>">尾页</a>
</p>
<script>
    // function goTo(t) {
    //     window.location.href="users.jsp?pageNo="+t.previousElementSibling.value;
    // }
    $(function () {
        $("#search").click(function () {
            $("#searchForm input[name=pageNo]").val(1);
        });
        $("#goto").click(function () {
            $("#searchForm input[name=pageNo]").val($(this).prev().val());
            $("#searchForm").submit();
        });
       $("#pager a").click(function () {
           //this是表示当前元素，是Dom元素
           let n=$(this).data("id");
           //$(this).prop("data-id");
           //alert(n);
           $("#searchForm input[name=pageNo]").val(n);
           $("#searchForm").submit();
           return false;
       });
    });
</script>
</body>
</html>
